<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of OriginizetionController
 *
 * @author jack
 */
class OriginizetionController extends ApiController
{

    /**
     * @ApiDoc(
     *      description="获得机构简介"
     * )
     */
    public function getAction($id)
    {
        $intro = $this->getEntityManager()->getRepository('WileadIArtistBundle:UserIntroduction')->createQueryBuilder('q')
            ->where('q.User = :id')
            ->setParameter('id', $id)
            ->getQuery()
            ->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

        $view = $this->getFOSView();
        if (!$intro)
            return $view->setData(array('userId' => $id));

        return $view->setData($intro);
    }

    /**
     * @ApiDoc(
     *      description="机构简介提交"
     * )
     * @RequestParam(name="id", requirements="integer", description="机构ID")
     * @RequestParam(name="title", requirements="string", description="机构名称")
     * @RequestParam(name="address", requirements="string", description="地址")
     * @RequestParam(name="permitId", requirements="string", description="许可证编号")
     * @RequestParam(name="description", requirements="string", description="简介")
     * @RequestParam(name="city", requirements="string", description="所在地")
     * @RequestParam(name="permitImg", requirements="string", description="证件图")
     * @RequestParam(name="otherPermitId", requirements="string", description="其他证件号码")
     * @RequestParam(name="otherPermitImg", requirements="string", description="其他证件图")
     */
    public function setAction()
    {
        $request = $this->getRequest()->request;
        $origin = $this->getEntityManager()->getRepository('WileadIArtistBundle:User')->findOneBy(array(
            'id' => $request->get('id')
            //'accountType' => \Wilead\IArtistBundle\Entity\User::ACCOUNT_TYPE_ORGANIZATION
        ));

        $view = $this->getFOSView();
        if (!$origin) {
            return $view->setData('不存在该机构')->setStatusCode(403);
        }

        $intro = $origin->getUserIntroduction();
        if (!$intro) {
            $intro = new \Wilead\IArtistBundle\Entity\UserIntroduction();
            $intro->setUser($origin);
        }

        $this->insertIntoEntity($request, $intro, array('title', 'address', 'permitId', 'otherPermitId', 'otherPermitImg', 'description', 'city', 'permitImg'));
        $em = $this->getEntityManager();
        $em->persist($intro);
        $em->flush();

        return RouteRedirectView::create('wilead_api_origanizetion_get', array('id' => $request->get('id')), 200);
    }

    /**
     * @ApiDoc(
     *      description="机构所有招聘职位"
     * )
     */
    public function jobAction($id)
    {
        return $this->getFOSView()->setStatusCode(200)->setData($this->getRepository('WileadIArtistBundle:Job')->getJob($id));
    }

    /**
     * @ApiDoc(
     *      description="机构目前招聘职位"
     * )
     */
    public function currentJobAction($id)
    {
        return $this->getFOSView()->setStatusCode(200)->setData($this->getRepository('WileadIArtistBundle:Job')->getJob($id, 'current'));
    }

    /**
     * @ApiDoc(
     *      description="机构历史招聘"
     * )
     */
    public function historyJobAction($id)
    {
        return $this->getFOSView()->setStatusCode(200)->setData($this->getRepository('WileadIArtistBundle:Job')->getJob($id, 'history'));
    }

}

?>